package juc.fulture;

import juc.util.Log;
import juc.util.ThreadUtil;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * @description:
 * @author: zk
 * @date: 2023-12-15
 */
public class FutureTest {
//    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
//        FutureTask<Integer> futureTask = new FutureTask<>(() -> {
//            Log.info("开始执行");
//            TimeUnit.SECONDS.sleep(3);
//            return 1;
//        });
//
//        Log.info("提交任务");
//        new Thread(futureTask,"thread1").start();
//
//        Integer integer = futureTask.get(1L,TimeUnit.SECONDS);
//        System.out.println(integer);
//    }

    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
        MyFutureTask<Integer> futureTask = new MyFutureTask<>(() -> {
            Log.info("开始执行");
            TimeUnit.SECONDS.sleep(3);
            Log.info("执行完毕");
            return 1;
        });

        Log.info("提交任务");
        new Thread(futureTask,"thread1").start();

        Log.info("处理别的任务");
        TimeUnit.SECONDS.sleep(1);
        Log.info("等待获取结果");

        Integer integer = futureTask.get();
        Log.info("获取到结果:"+integer);
    }
}
